package com.sxpcwlkj.system.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import com.sxpcwlkj.framework.constant.RedisConstant;
import com.sxpcwlkj.common.code.controller.BaseController;
import com.sxpcwlkj.common.code.entity.Constants;
import com.sxpcwlkj.system.entity.AdminMenuTree;
import com.sxpcwlkj.system.entity.bo.LoginBodyBo;
import com.sxpcwlkj.system.entity.vo.SysUserVo;
import com.sxpcwlkj.framework.config.LoginObject;
import com.sxpcwlkj.system.service.SysLoginService;
import com.sxpcwlkj.system.service.SysUserService;
import com.sxpcwlkj.common.utils.AjaxResult;
import com.sxpcwlkj.common.utils.RedisUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author shanpengnian
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("auth")
public class AuthController extends BaseController {


    private final SysLoginService loginService;
    private final SysUserService sysUserService;


    /**
     * 登录方法
     */
    @SaIgnore
    @PostMapping("/login")
    public AjaxResult<Map<String, Object>> login(@Validated @RequestBody LoginBodyBo loginBodyBo) {
        Map<String, Object> ajax = new HashMap<>(16);
        // 生成令牌
        String token = loginService.login(loginBodyBo);
        ajax.put(Constants.TOKEN, token);
        SysUserVo sysUser = sysUserService.getUserRoleAnfFunctionInfo(LoginObject.getLoginId());
        Map<String,Object> userInfo=new HashMap<>();
        userInfo.put("userName",sysUser.getUserName());
        userInfo.put("photo",sysUser.getAvatar());
        // 缓存登录对象 authBtnList
        userInfo.put("roles", sysUser.getRoleCodes());
        userInfo.put("authBtnList", sysUser.getButCodes());
        userInfo.put("time",new Date().getTime());
        userInfo.put("nickName",sysUser.getNickName());
        userInfo.put("email",sysUser.getEmail());
        userInfo.put("phoneNumber",sysUser.getPhoneNumber());
        userInfo.put("sex",sysUser.getSex());
        userInfo.put("loginIp",sysUser.getLoginIp());
        userInfo.put("loginDate",sysUser.getLoginDate());
        userInfo.put("roleName",sysUser.getRoleName());
        ajax.put(Constants.USERINFO, userInfo);
        RedisUtil.setCacheObject(RedisConstant.USER_KEY+sysUser.getUserId(),sysUser, Duration.ofHours(24));
        return success(ajax);
    }

    /**
     * 退出登录
     */
    @SaIgnore
    @PostMapping("/logout")
    public AjaxResult<String> logout() {
        loginService.logout();
        return success("退出成功");
    }


    /**
     * 用户角色/资源
     */
    @SaCheckLogin
    @PostMapping("/getUserRoleAndFunctionInfo")
    public AjaxResult<SysUserVo> getUserRoleAndFunctionInfo() {
        SysUserVo userRoleAnfFunctionInfo = sysUserService.getUserRoleAnfFunctionInfo("1");
        return success(userRoleAnfFunctionInfo);
    }

    /**
     * 系统菜单
     */
    @SaCheckPermission("system:model:menu")
    @GetMapping("/getMenu")
    public AjaxResult<Object> getMenu() {
        List<AdminMenuTree> menuTree = sysUserService.getAdminMenuTree(LoginObject.getLoginId());
        return success(menuTree);
    }



}
